<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="close">
    <!-- 表单 -->
    <el-form ref="form" :model="formData" label-width="120px" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式">
        <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" /></el-form-item>

      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:80%" placeholder="请选择部门" @focus="showTree" />
        <el-tree v-show="showtree" id="tree" :data="department" :props="defaultProps" @node-click="handleNodeClick" />

      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="close">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import employee from '@/api/constant/employees.js'
import { department } from '@/api/department.js'
import { addEmployee } from '@/api/employees.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      // 默认值false
      default: false
    }
  },
  data() {
    return {
      showtree: false,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间为空', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '转正时间为空', trigger: 'blur' }]
      },
      hireType: [],
      department: [],
      data: [{
        label: '一级 1',
        children: [{
          label: '二级 1-1',
          children: [{
            label: '三级 1-1-1'
          }]
        }]
      }, {
        label: '一级 2',
        children: [{
          label: '二级 2-1',
          children: [{
            label: '三级 2-1-1'
          }]
        }, {
          label: '二级 2-2',
          children: [{
            label: '三级 2-2-1'
          }]
        }]
      }, {
        label: '一级 3',
        children: [{
          label: '二级 3-1',
          children: [{
            label: '三级 3-1-1'
          }]
        }, {
          label: '二级 3-2',
          children: [{
            label: '三级 3-2-1'
          }]
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  async created() {
    this.hireType = employee.hireType
    // 获取树型结构的部门
    const { depts } = await department()
    this.department = depts.filter((ele) => {
      // 改造数据
      depts.forEach((el) => {
        if (el.pid === ele.id) {
          // 如果ele.children存在则继续使用无事发生，没有则生成一个新数组
          ele.children = ele.children || []
          ele.children.push(el)
        }
      })
      return ele.pid === ''
    })
  },
  methods: {
    // 点击确认提交表单
    async btnOK() {
      // console.log(this.formData)
      await this.$refs.form.validate()
      await addEmployee(this.formData)
      this.$message.success('增加成功')
      this.$parent.getEmployeeList()
      this.$emit('update:showDialog', false)
    },
    // 关闭
    close() {
      this.$emit('update:showDialog', false)
    },
    // 关闭

    // 部门树形结构
    handleNodeClick(val) {
      this.showtree = false
      this.formData.departmentName = val.name
    },
    showTree() {
      this.showtree = true
    }
    // 部门树形结构
  }

}
</script>
<style lang="scss" scoped>
    #tree{
      background-color: red;
    }
</style>

